package cn.iocoder.yudao.module.shop.staff.dao;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.shop.staff.pojo.dto.ShopStaffPageDTO;
import cn.iocoder.yudao.module.shop.staff.pojo.po.ShopStaff;
import jakarta.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Objects;

/**
 * 店员 Mapper
 *
 * @author 山野羡民
 */
@Mapper
public interface ShopStaffMapper extends BaseMapperX<ShopStaff> {

    default PageResult<ShopStaff> selectPage(ShopStaffPageDTO reqVO) {
        return selectPage(reqVO, new CoreLambdaQueryWrapper<ShopStaff>()
                .eqIfPresent(ShopStaff::getShopId, reqVO.getShopId())
                .likeIfPresent(ShopStaff::getContactName, reqVO.getContactName())
                .eqIfPresent(ShopStaff::getContactMobile, reqVO.getContactMobile())
                .eqIfPresent(ShopStaff::getMemberUserId, reqVO.getMemberUserId())
                .eqIfPresent(ShopStaff::getAdminUserId, reqVO.getAdminUserId())
                .betweenIfPresent(ShopStaff::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(ShopStaff::getShopId)
                .orderByDesc(ShopStaff::getId));
    }

    default List<ShopStaff> selectByShopId(Long shopId, @Nullable Integer role) {
        CoreLambdaQueryWrapper<ShopStaff> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(ShopStaff::getShopId, shopId);
        wrapper.apply(role != null && role > 0, "FIND_IN_SET({0}, roles) > 0", role);
        return selectList(wrapper);
    }

    default List<ShopStaff> selectByMemberUserId(Long memberUserId) {
        return selectList(ShopStaff::getMemberUserId, memberUserId);
    }

    default ShopStaff selectByMemberUserId(Long shopId, Long memberUserId) {
        return selectOne(ShopStaff::getShopId, shopId, ShopStaff::getMemberUserId, memberUserId);
    }

    default List<ShopStaff> selectByAdminUserId(Long adminUserId) {
        return selectList(ShopStaff::getAdminUserId, adminUserId);
    }

    default ShopStaff selectByAdminUserId(Long shopId, Long adminUserId) {
        return selectOne(ShopStaff::getShopId, shopId, ShopStaff::getAdminUserId, adminUserId);
    }

    default List<ShopStaff> selectByMobile(String mobile) {
        return selectList(ShopStaff::getContactMobile, mobile);
    }

    default ShopStaff selectByMobile(Long shopId, String mobile) {
        return selectOne(ShopStaff::getShopId, shopId, ShopStaff::getContactMobile, mobile);
    }

    default void deleteByUserId(Long userId, Integer userType) {
        if (Objects.equals(userType, UserTypeEnum.MEMBER.getValue())) {
            delete(ShopStaff::getMemberUserId, userId);
            return;
        }
        delete(ShopStaff::getAdminUserId, userId);
    }

}